﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HHCommon;
using System.IO;

namespace HHWeb.SystemManage
{
    public partial class Right : System.Web.UI.Page
    {
        Help help = new Help();
        HHBLL.Users B = new HHBLL.Users();
        HHModel.Users M = new HHModel.Users();
        EncAndDec EncAndDec = new EncAndDec();

        //定义变量
        public string UserNumber = string.Empty;
        public string UserName = string.Empty;
        public string UserCode = string.Empty;
        public string UserPicUrl = string.Empty;
        public string UserTel = string.Empty;
        public string UserEmail = string.Empty;
        public string UserType = string.Empty;
        public int OpenState = 1;//用户状态（0：禁用1：启用）
        public int Sort = 0;//排序号
        public string Signature = string.Empty;
        public string UserAddress = string.Empty;
        public string Creater = string.Empty;
        public string CreateTime = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (help.GetHttpMethodBool())
                {
                    string message = string.Empty;
                    message = UpdateUserInfo();
                    MessageBox.ShowAndRedirect(this.Page, message, "Right.aspx");
                }
                else
                {
                    UserNumber = EncAndDec.DESDecrypting(help.GetCookie("UserNumber"));
                    M = B.GetModelByUserNumber(UserNumber);
                    if (M != null)
                    {
                        UserNumber = M.UserNumber;
                        UserName = M.UserName;
                        UserCode = M.UserCode;
                        Signature = M.Signature;
                        UserPicUrl = "../MYFiles/UserImg/" + M.UserPicUrl;
                        UserTel = M.UserTel;
                        UserEmail = M.UserEmail;
                        UserType = M.UserType;
                        OpenState = Convert.ToInt32(M.OpenState);
                        Sort = Convert.ToInt32(M.Sort);
                        UserAddress = M.UserAddress;
                        Creater = M.Creater;
                        CreateTime = Convert.ToDateTime(M.CreateTime).ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    else
                    {
                        Response.Write("<script language =\"javascript\">alert('对不起！请重新登录');window.open('/SystemManage/Login.aspx','_top')</script>");
                        return;
                    }
                }
            }
            catch
            {
                Response.Write("<script language =\"javascript\">alert('对不起！请重新登录');window.open('/SystemManage/Login.aspx','_top')</script>");
                return;
            }
        }

        #region  //修改用户信息
        public string UpdateUserInfo()
        {
            M.UserNumber = Request.Form["UserNumber"].ToString();
            M.UserName = Request.Form["UserName"].ToString();
            M.UserCode = Request.Form["UserCode"].ToString();
            #region //图片上传
            HttpPostedFile hpf = upload_img.PostedFile;
            string fileName = Path.GetFileName(hpf.FileName);
            string ex = Path.GetExtension(fileName);//获取文件后缀名
            string SavaUrl = string.Empty;
            string OldImgUrl = Request.Form["ImgUrl"].ToString();
            string rd = help.GetRandom();
            if (!string.IsNullOrEmpty(fileName))
            {
                if (ex == ".gif" || ex == ".jpg" || ex == ".bmp" || ex == ".png")
                {
                    SavaUrl = "../MYFiles/UserImg/" +rd+ex;
                    if (System.IO.File.Exists(Server.MapPath(OldImgUrl)))
                        System.IO.File.Delete(Server.MapPath(OldImgUrl));
                    hpf.SaveAs(Server.MapPath(SavaUrl));//保存图片路经
                }
                else
                    return "图片格式错误！";
                M.UserPicUrl = rd+ex;
            }
            else
                M.UserPicUrl = OldImgUrl.Remove(0,OldImgUrl.LastIndexOf("/") + 1);
            #endregion
            M.Signature = Request.Form["Signature"].ToString();
            M.UserTel = Request.Form["UserTel"].ToString();
            M.UserNumber = Request.Form["UserNumber"].ToString();
            M.UserEmail = Request.Form["UserEmail"].ToString();
            M.UserAddress = Request.Form["UserAddress"].ToString();
            M.LastUpdater = EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
            M.LastUpdateTime = DateTime.Now;
            if (B.UpdateByUserNumber(M))
                return "修改成功！";
            else
                return "修改失败！";
        }
        #endregion
    }
}